package com.example.xd.hotfixdemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTv1;
    private TextView mTv2;
    private TextView mTv3;
    private Test1 mLala;
    private EditText mEtName;
    private EditText mEtAge;
    private TextView mTv5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPower();
        initView();

    }

    public void requestPower() {
        //判断是否已经赋予权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {//这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限
            } else {
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,}, 1);
            }
        }
    }

    private void initView() {
        mTv1 = findViewById(R.id.tv1);
        mTv2 = findViewById(R.id.tv2);
        mTv3 = findViewById(R.id.tv3);
        mTv5 = findViewById(R.id.tv5);
        mEtName = findViewById(R.id.et_name);
        mEtAge = findViewById(R.id.et_age);
        mTv1.setOnClickListener(this);
        mTv2.setOnClickListener(this);
        mTv3.setOnClickListener(this);
        findViewById(R.id.tv4).setOnClickListener(this);
        mTv5.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:
                if (mLala != null) {
                    mTv1.setText(mLala.toString());
                } else {
                    Toast.makeText(this, "先生成对象", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv2:
                if (mLala != null) {
                    mTv2.setText("姓名：" + mLala.getName());
                } else {
                    Toast.makeText(this, "先生成对象", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv3:
                if (mLala != null) {
                    mTv3.setText("年龄" + mLala.getAge());
                } else {
                    Toast.makeText(this, "先生成对象", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv4:
                String name = mEtName.getText().toString().trim();
                String age = mEtAge.getText().toString().trim();
                mLala = new Test1(name, age);
                Toast.makeText(this, "生成对象成功", Toast.LENGTH_SHORT).show();

                break;
            case R.id.tv5:
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
                break;
        }
    }
}
